package com.transpeed.payservice.pay.unionpay;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.json.JSONObject;

import com.transpeed.payservice.Utils.JsonUtils;
import com.transpeed.payservice.Utils.MD5Utils;
import com.transpeed.payservice.log.Log;

public class CloseUmsPayQRCode {
	public static final String ADD_URL = "http://umspay.izhong.me/netpay-route-server/api/";

	public static void test_trade_CloseUnionQRCode(String qrCodeId) {
		try {
			// 创建连接
			URL url = new URL(ADD_URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.connect();
			// POST请求
			SimpleDateFormat requestTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			SimpleDateFormat qrCodeIdTimeFormat = new SimpleDateFormat("yyyyMMddmmHHssSSS");// 设置日期格式
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());

		
			String RequestTimestamp = requestTimeFormat.format(new Date());

			HashMap<String, Object> test = new HashMap<String, Object>();

			test.put("msgId", "800000000010");
			test.put("msgSrc", "WWW.TRANSPEEDSG.COM");
			test.put("msgType", "bills.closeQRCode");
			test.put("requestTimestamp", RequestTimestamp);
			test.put("srcReserve", "1234");
			test.put("mid", "911000000000000");
			test.put("tid", "88880001");
			test.put("instMid", "QRPAYDEFAULT");

			test.put("qrCodeId", qrCodeId);
			test.put("systemId", "3075");
			// MD5Utils.getLexicographicalOrder(test,"DSANEdAEJCDNTDaS7ye8jFfBSzNGTZkR5SFzHfn8TWkSd5fK");
			test.put("sign", MD5Utils.getSignature(test,"DSANEdAEJCDNTDaS7ye8jFfBSzNGTZkR5SFzHfn8TWkSd5fK"));
			
			Log.Write(RequestTimestamp, "UmsPay");
			

			Log.Write("发送Json为 \n" + JsonUtils.formatAsJSON(new JSONObject(test).toString()),"UmsPay");
			byte [] bytes =  new JSONObject(test).toString().getBytes("UTF-8");
			out.write(bytes);
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "gbk");
				sb.append(lines);
			}

			reader.close(); 
			// 断开连接
			connection.disconnect();

			Log.Write("接收Json为 \n" + JsonUtils.formatAsJSON(sb.toString()),"UmsPay");

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		test_trade_CloseUnionQRCode("3075201704100118365770903188");
	}
}
